import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/index'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/login', component: () => import('@/views/login/loginPage.vue') },
    {
      path: '/',
      component: () => import('@/views/layout/layoutContainer.vue'),
      redirect: '/article/manage',
      children: [
        { path: '/article/manage', component: () => import('@/views/article/articleManage.vue') },
        { path: '/article/channel', component: () => import('@/views/article/articleChannel.vue') },
        { path: '/user/profile', component: () => import('@/views/user/userProfile.vue') },
        { path: '/user/avator', component: () => import('@/views/user/userAvatar.vue') },
        { path: '/user/password', component: () => import('@/views/user/userPassword.vue') },
      ],
    },
  ],
})
// 添加登录访问拦截
router.beforeEach((to) => {
  // 如果没有token，且访问的是非登录页，拦截到登录页，其他情况放行
  const userStore = useUserStore()
  if (!userStore.token && to.path !== '/login') return '/login'
})

export default router
